;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.Turnover", [])
        .controller("TurnoverCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.datePickerFlag = [false, false];

            $scope.condition = {};
            $scope.dataList = [];

            var datePickModeObj = {
                minMode:'month',
                datepickerMode: 'month'
            };
            $scope.dateOptions = $scope.isTJ ? datePickModeObj : {};

            $scope.switchDatePicker = function (index) {
                if (index == 1 && $scope.condition.monthStart == null) {
                    ybWidget.ShowMessage("请选择初始查询时间", MESG_TYPE.Error);
                    return;
                }
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };
            $scope.tbConf = {
                scrollWidth: 1070,
                tableCol: [{
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDesc",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },

                    {
                        type: "",
                        name: "期初库存数量",
                        dataField: "beginAmount",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "期末库存数量",
                        dataField: "endAmount",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "开始月份",
                        dataField: "startMonth",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "结束月份",
                        dataField: "endMonth",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库量",
                        dataField: "outAmount",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "库存周转天数",
                        dataField: "turnoverDays",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                ]
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/warehouseInventory/turnover/export")
            };

            $scope.reset = function () {
                $scope.condition = {};
                $scope.timeInterval = undefined;
            };

            var btnPermissionCode = {
                turnoverExport: window.ActionCode.turnoverExport
            };

            $scope.btnPermission = {
                turnoverExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }
            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                    $scope.condition.monthStart = $filter("YbDate")(obj[startFiled], 'yyyy-MM');
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                    $scope.timeInterval = (new Date(obj[endFiled]).getTime() - new Date(obj[startFiled]).getTime())/ (1000*3600*24);
                    if($scope.timeInterval >= 368){
                        ybWidget.ShowMessage("查询时间间隔需小于一年", MESG_TYPE.Error);
                        return;
                    }
                    $scope.condition.monthEnd = $filter("YbDate")(obj[endFiled], 'yyyy-MM');
                }
            };

            function getDataList(curPage) {
                if($scope.timeInterval >= 366){
                    ybWidget.ShowMessage("查询时间间隔需小于一年", MESG_TYPE.Error);
                    return;
                }
                $scope.markInfo.curRow = -1;
                var service = "device";
                var reqUrl = "/warehouseInventory/turnover/report";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);